﻿<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Title</title>
	<script src="../js/jquery.min.js"></script>
	<script src="../js/jquery.easyui.min.js"></script>
	<script src="../js/easyui-lang-zh_CN.js"></script>
	<link rel="stylesheet" href="../themes/icon.css">
	<link rel="stylesheet" href="../themes/default/easyui.css">
	<link rel="stylesheet" href="../css/wu.css">
	<script>
        var  selectIndex = -1;

        /*生成detailDatagrid方法*/
        function loadDetail(rowCode) {
            $("#detail").datagrid({
                url:"../../collectBillController/loadDetailInFirstPage",
                queryParams: {code: rowCode},
                rownumbers:true,
                checkOnSelect:false,
                columns:[[
                    {field:'billCode',title:'业务单号',width: 130},
                    {field:'a',title:'本方账目',formatter: function (value,row,index) {
                            return "应收账款";
                        }},
                    {field:'b',title:'本方增减',formatter: function (value,row,index) {
                            return "增加";
                        }},
                    {field:'collectMoney',title:'应收金额'},
                    {field: "realMoney",title: "实收金额"},
                    {field: "memo",title: "备注",width: 200}
                ]],
                height: 145,
                onLoadSuccess: function () {
                    var rows=$("#detail").datagrid("getRows");
                    var collectMoenySum=0;
                    var realMoneySum=0;
                    for (var i=0;i<rows.length;i++){
                        collectMoenySum+=rows[i].collectMoney;
                        realMoneySum+=rows[i].realMoney;
                    }
                    $("#detail").datagrid("appendRow",{'billCode': "合计",'a':"",'b':"",
                        'collectMoney': collectMoenySum,'realMoney': realMoneySum,"memo": ""});
                }
            });

        }

        /*生成excel表格的方法*/
        function getExcel() {
            /*$.ajax({
                url: "../../collectBillController/getExcel",
                data: {data:data}
            });*/
            $.messager.confirm('确认','您确认想要导出excel文件吗？（下载路径为浏览器默认下载路径）',function(r) {
                if (r) {
                    var findcode=$("#findcode").val();
                    var findstartDate=$("#findstartDate").val();
                    var findendDate=$("#findendDate").val();
                    var findsupplierName=$("#findsupplierName").val();
                    location.href="../../collectBillController/getExcel?findcode="+findcode+"&findstartDate="+findstartDate
                        +"&findendDate="+findendDate+"&findsupplierName="+findsupplierName;
                }
            });
        }

        /*删除按钮的删除方法*/
        function deleteCollectBillList() {
            var rows=$("#productList").datagrid('getChecked');
            $.messager.confirm('确认','您确认想要删除吗？',function(r) {
                if (r) {
                    if (rows.length==0){
                        alert('请选择');
                        return false;
                    }
                    for (var i=0;i<rows.length;i++){
                        deleteCollectBill($("#productList").datagrid('getRowIndex',rows[i].code));
                    }
                }
            });
            /*refreshDatagrid();*/
        }
        /*行中的删除方法*/
        function deleteCollectBill_btn(index) {
            $.messager.confirm('确认','您确认想要删除吗？',function(r) {
                if (r) {
                    deleteCollectBill(index);
                }
            });
            /*refreshDatagrid();*/
        }

        /*通用删除方法*/
        function deleteCollectBill(index){
            var rows = $('#productList').datagrid('getRows');
            var row=rows[index];
            $.ajax({
                type: "post",
                url: "../../collectBillController/deleteCollectBill",
                data:{collectCode: row.code,collectStatus: row.collectStatus},
                dataType: "text",
                success: function (data) {
                    /*alert(data);*/
                    refreshDatagrid();
                }
            });
        }

        function editCurrentCollectBill(index,value){
            var rows = $('#productList').datagrid('getRows');
            var row = rows[index];
            var value_1=rows[index].code;
            if(row.collectStatus==1){
                addDetail(value_1);
            }else if(row.collectStatus==2){
                addDetailChecked(value_1);
            }else if(row.collectStatus==3){
            	addDetailPaied(value_1);
            }
        }

        function loadDatagrid_pro(params){
            $("#productList").datagrid({
                height: 320,
                url:'../../collectBillController/reLoadDatagird',
                rownumbers:true,
                checkOnSelect:false,
                onSelect:function(rowIndex, rowData){  //用于解决点击某行不会高亮
                    $('#productList').datagrid('unselectRow',rowIndex);
                },
                idField: "code",
                columns:[[
                    {field:'ck',checkbox:true},
                    {field:'code',title:'收款单号',formatter:function(value,row,index){
                            var context="<a href='#'>"+value+"</a>";
                            return context;
                        }},
                    {field:'collectDate',title:'收款日期'},
                    {field:'customName',title:'单位名称'},
                    {field:'collectTotalMoney',title:'收款金额'},
                    {field: "collectType",title: "收款方式",formatter:function(value){
                            if(value==1){
                                return "现金";
                            }else if(value==2){
                                return "转账";
                            }
                        }},
                    {field: "collectStatus",title: "支付状态",formatter: function(value){
                            if(value==1){
                                return "未审核";
                            }else if(value==2){
                                return "已审核";
                            }else if(value ==3){
                                return "已收款";
                            }
                        },styler: function(value,row,index){
                            if(value==1){
                                return 'color: red';
                            }else if(value==2){
                                return "color: black";
                            }else if(value ==3){
                                return 'color: green';
                            }
                        }
                    },
                    {field: "memo",title: "备注"},
                    {field: "staff",title: "操作员",formatter:function(value,row,index){return "${user.userName}";}},
                    {field:'opt',title:'操作',width:200,formatter:function (value,row,index) {
                            var context="<a href='#' onclick='editCurrentCollectBill("+index+","+value+")'>编辑</a>&nbsp;&nbsp;" +"<a href='#' onclick='deleteCollectBill_btn("+index+")'>删除</a>";
                            return context;
                        },styler: function(value,row,index){return 'color: blue';}}
                ]],
                toolbar:'#wu_toolbar',
                queryParams: params,
                pagination:true ,//向后台传递参数page 和rows
                pageNumber: 1,
                pageSize: 5,
                pageList: [5,10,20],
                onClickCell:function(index, field, value){
                    if(field=='code'){
                        var rows = $('#productList').datagrid('getRows');
                        var row = rows[index];
                        if(row.collectStatus==1){
                            addDetail(value);
                        }else if(row.collectStatus==2){
                            addDetailChecked(value);
                        }else if (row.collectStatus==3) {
                            addDetailPaied(value);
                        }
                    }else{
                        return false;
                    }
                },
                onDblClickRow: function (index, row) {
                    $("#detail_div").css("display","block");
                    $("#detail_div p").text("单据标号为："+row.code+" 的明细如下所列");
                    loadDetail(row.code);

                }
            });
        }

        function searchSubmit(){
            $.messager.progress();
            var params={
                findcode: $("#findcode").val(),
                findstartDate: $("#findstartDate").val(),
                findendDate: $("#findendDate").val(),
                findsupplierName:$("#findsupplierName").val()
            };
            loadDatagrid_pro(params);
            $.messager.progress('close');

        }

        function hideTable(){
            $("#searchdiv").toggle();
        }

        function resetForm(){
            $("#form1").form('reset');
        }

        $(function(){

            loadDatagrid_pro(null);

            $("#search_btn").click(function(){
                searchSubmit();
            });

            //点击tab刷新功能
            /*self.parent.$("#jxc_tabs").tabs({
                onSelect: function (title,index) {
                    if (title == "销售收款管理") {
                        $("#productList").datagrid('reload');
                    }
                }
            });*/

        })
        function addProduct() {
            parent.addTabs("添加收款单","goods/add_CollectBill.html");
        }

        function addDetail(value) {
            $.ajax({
                url: "../../collectBillController/findCollectBill",
                type: "post",
                data:{collectCode:value},
                dataType: "text",
                success: function (data) {
                    parent.addTabs("销售付款单","goods/collectbill_detail.html");
                }
            });

        }

        function addDetailChecked(value){
            $.ajax({
                type: "post",
                url: "../../collectBillController/findCollectBill",
                data:{'collectCode':value},
                dataType: "text",
                success: function (data) {
                    parent.addTabs("销售收款单","goods/collectbill_detail_checked.html");
                }
            });

        }

        function  addDetailPaied(value) {
            $.ajax({
                url: "../../collectBillController/findCollectBill",
                type: "post",
                data:{collectCode:value},
                dataType: "text",
                success: function () {
                    parent.addTabs("销售收款单","goods/collectbill_detail_paied.html");
                }
            });

        }

        function refreshDatagrid(){
            $("#productList").datagrid('reload');
        }

        function del() {
            var rows=$("#productList").datagrid('getChecked');
            if(rows.length==0){
                $.messager.alert('消息',"请选择记录");
                return
            }
            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){

                }
                var code=new Array();
                for( var i=0;i<rows.length;i++){
                    code[i]=rows[i].code;
                }
                $.ajax({
                    url: "../../collectBillController/deleteCollectBill",
                    data: {"code":code},
                    success: function(data){
                        if(data=="success"){
                            alert("删除成功");
                        }
                    }
                });
                $("#productList").datagrid('reload');
            });

        }
	</script>

</head>
<body>
<div id="wu_toolbar">
	<div class="wu_toolbar_button">
		<a href="#" onclick="hideTable()" class="easyui-linkbutton"
		   data-options="iconCls:'icon-search'" plain="true">查询</a>| <a
			href="#" onclick="addProduct()" class="easyui-linkbutton"
			data-options="iconCls:'icon-add'" plain="true">添加</a>| <a href="#"
																	  onclick="deleteCollectBillList()" class="easyui-linkbutton"
																	  data-options="iconCls:'icon-delete-self'" plain="true">删除</a>| <a
			href="#" class="easyui-linkbutton"
			data-options="iconCls:'icon-page-refresh'" plain="true"
			onclick="refreshDatagrid()">刷新</a>| <a href="#"
												   class="easyui-linkbutton"
												   data-options="iconCls:'icon-communication'" plain="true" onclick="getExcel()">导出excal</a>
	</div>
	<div id="searchdiv" style="display: block; width: 100%;">
		<form name="form1" id="form1">

			<table width="100%" border="0" cellspacing="0"
				   style="background-color: #c9dfff;">
				<tbody>
				<tr>
					<td width="70"><b>检索条件:</b></td>
					<td width="70" align="right">询价编号:</td>
					<td width="150" align="left"><input name="findcode"
														class="easyui-textbox" type="text" id="findcode" value=""
														maxlength="100"></td>
					<td width="70" align="right">开始日期:</td>
					<td width="150" align="left"><div class="l-text-wrapper">
						<div class="l-text l-text-date">
							<input name="findstartDate" type="text" id="findstartDate"
								   value="" maxlength="30" class="easyui-datebox"
								   ligeruiid="findstartDate">
							<div class="l-text-l"></div>
							<div class="l-text-r"></div>
							<div class="l-trigger">
								<div class="l-trigger-icon"></div>
							</div>
						</div>
					</div></td>
					<td width="70" align="right">结束日期:</td>
					<td width="150" align="left"><div class="l-text-wrapper">
						<div class="l-text l-text-date">
							<input name="findendDate" type="text" id="findendDate"
								   value="" maxlength="30" class="easyui-datebox"
								   ligeruiid="findendDate">
							<div class="l-text-l"></div>
							<div class="l-text-r"></div>
							<div class="l-trigger">
								<div class="l-trigger-icon"></div>
							</div>
						</div>
					</div></td>
					<td width="70" align="right">供应商名:</td>
					<td width="150" align="left"><input name="findsupplierName"
														class="easyui-textbox" type="text" id="findsupplierName"
														value="" maxlength="100"></td>
					<td width="72"><a id="search_btn" href="#"
									  class="easyui-linkbutton" data-options="iconCls:'icon-search'">搜索</a>
					</td>
					<td width="72"><a id="reset_btn" href="#"
									  class="easyui-linkbutton" data-options="iconCls:'icon-refresh'"
									  onclick="javascript:resetForm()">重置</a></td>
					<td width="0"></td>


				</tr>
				</tbody>
			</table>
			<input type="hidden" id="menuCode" value="100001001">
		</form>
	</div>
</div>
<div id="productList" class="easyui-datagrid"></div>
<div id="detail_div" style="display: none">
	<p></p>
	<div id="detail" class="easyui-datagrid"></div>
</div>
</body>
</html>